package com.gxa.service.impl;

import com.gxa.exception.CustomerException;
import com.gxa.mapper.PatientMapper;
import com.gxa.pojo.Patient;
import com.gxa.service.PatientService;
import com.gxa.util.Md5Utils;
import com.gxa.util.QueryInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/09/22
 * Description
 * Version: V1.0
 */
@Service("patientService")
@Transactional
public class PatientServiceImpl implements PatientService {

    @Resource
    PatientMapper patientMapper;


    @Override
    public boolean addPatient(Patient patient) throws CustomerException {
        System.out.println("注册开始");
        System.out.println(patient);
        if (patient.getUsername().equals("") || patient.getUsername() == null) {
            throw new CustomerException("用户名不能为空");
        }
        if (patient.getPhone() == null || patient.getPhone().equals("")) {
            throw new CustomerException("手机号不能为空");
        }
        //根据患者的注册信息来查询信息是否已经注册，如果已经注册将不能再次注册
        if (patientMapper.findByPhone(patient.getPhone()) != null) {
            throw new CustomerException("手机号已经存在");
        }
        if (patientMapper.findByUserName(patient.getUsername()) != null) {
            throw new CustomerException("用户名已经存在");
        }
        //UUID生成盐，用于密码加密
        String salt = Md5Utils.createSalt();
        patient.setSalt(salt);
        //盐和密码进行加密
        String password = Md5Utils.md5Password(patient.getPassword(), patient.getSalt());
        //密码设置为加密后的密码存储
        patient.setPassword(password);
        //添加患者信息
        patientMapper.insert(patient);
        System.out.println("注册结束");
        return true;
    }

    @Override
    public Patient login(Map<String,Object> map) throws CustomerException {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        if (username==null||username.equals("")){
            throw new CustomerException("用户名不能为空");
        }
        if (password==null||password.equals("")){
            throw new CustomerException("密码不能为空");
        }
        //首先通过用户名查询获取盐
        Patient byUserName = patientMapper.findByUserName(username);
        System.out.println("salt值:"+byUserName.getSalt());
        //对密码进行加密处理
        String md5Password = Md5Utils.md5Password(password, byUserName.getSalt());
        //通过用户名和密码查询患者是否存在
        Patient byUserNameAndPassword = patientMapper.findByUserNameAndPassword(username, md5Password);
        if (byUserNameAndPassword==null){
            throw new CustomerException("用户名或密码错误");
        }
        return byUserNameAndPassword;
    }

    @Override
    public Integer insert(Patient patient) {
        if (patient == null) {
            return null;
        }

        return patientMapper.insert(patient);
    }

    @Override
    public Patient findById(Integer id) {
        return patientMapper.findById(id);
    }

    @Override
    public Integer findByAddress(String address) {
        return patientMapper.findByAddress(address);
    }

    @Override
    public List<Patient> findByDoctorId(Integer doctorId) {
        List<Patient> byDoctorId = patientMapper.findByDoctorId(doctorId);
        return byDoctorId;
    }

    @Override
    public List<Patient> findAll() {
        return patientMapper.findAll();
    }

    @Override
    public List<Patient> findByPage(QueryInfo queryInfo) {
        return patientMapper.findByPage(queryInfo.getStartIndex(),queryInfo.getPagesize());
    }

    @Override
    public Integer update(Patient patient) {

        //TODO
//        if (patient.getPassword()!=null&&!patient.getPassword().equals("")){
//            String salt = Md5Utils.createSalt();
//            patient.setSalt(salt);
//            String md5Password = Md5Utils.md5Password(patient.getPassword(), salt);
//            patient.setPassword(md5Password);
//        }
        return patientMapper.update(patient);
    }


    @Override
    public Integer addAcount(Integer acount,Integer id) {
        return patientMapper.addAcount(acount,id);
    }

    @Override
    public Integer deductAcount(Integer acount,Integer id) {
        return patientMapper.deductAcount(acount,id);
    }

}
